<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

global $dal, $premium;
$query = "SELECT auto_refresh " .
		"FROM users " .
		"WHERE user_id = " . $_SESSION[ 'user_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);

aos_header( 'Trade', ( $row[ 'auto_refresh' ] == 1 ) );
?>
					<table id="values">
<?php
$result2 = mysql_query( "SELECT coal_gold, iron_gold, food_gold, turn " .
		"FROM game" ) or die ( "Query failed: " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
$current_turn = $row2[ 'turn' ];
$gold_iron = 1 / $row2[ 'iron_gold' ];
$gold_coal = 1 / $row2[ 'coal_gold' ];
$gold_food = 1 / $row2[ 'food_gold' ];
$iron_coal = $row2[ 'iron_gold' ] / $row2[ 'coal_gold' ];
$coal_iron = 1 / $iron_coal;
$food_coal = $row2[ 'food_gold' ] / $row2[ 'coal_gold' ];
$coal_food = 1 / $food_coal;
$iron_food = $row2[ 'iron_gold' ] / $row2[ 'food_gold' ];
$food_iron = 1 / $iron_food;
?>
						<tr>
							<td width="25%">&nbsp;</td>
							<td align="right">
								<strong class="gold"><?php transprint( 'Gold' ); ?></strong>
							</td>
							<td align="right">
								<strong class="iron"><?php transprint( 'Iron' ); ?></strong>
							</td>
							<td align="right">
								<strong class="coal"><?php transprint( 'Coal' ); ?></strong>
							</td>
							<td align="right">
								<strong class="food"><?php transprint( 'Food' ); ?></strong>
							</td>
						</tr>
						<tr>
							<td>
								<strong class="gold"><?php transprint( 'Gold' ); ?></strong>
							</td>
							<td align="right">
								<strong class="gold">1</strong>
							</td>
							<td align="right">
								<strong class="iron"><?php print( round( $gold_iron, 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="coal"><?php print( round( $gold_coal, 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="food"><?php print( round( $gold_food, 2 ) ); ?></strong>
							</td>
						</tr>
						<tr>
							<td>
								<strong class="iron"><?php transprint( 'Iron' ); ?></strong>
							</td>
							<td align="right">
								<strong class="gold"><?php print( round( $row2[ 'iron_gold' ], 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong id="iron">1</strong>
							</td>
							<td align="right">
								<strong class="coal"><?php print( round( $iron_coal, 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="food"><?php print( round( $iron_food, 2 ) ); ?></strong>
							</td>
						</tr>
						<tr>
							<td>
								<strong class="coal"><?php transprint( 'Coal' ); ?></strong>
							</td>
							<td align="right">
								<strong class="gold"><?php print( round( $row2[ 'coal_gold' ], 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="iron"><?php print( round( $coal_iron, 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="coal">1</strong>
							</td>
							<td align="right">
								<strong class="food"><?php print( round( $coal_food, 2 ) ); ?></strong>
							</td>
						</tr>
						<tr>
							<td>
								<strong class="food"><?php transprint( 'Food' ); ?></strong>
							</td>
							<td align="right">
								<strong class="gold"><?php print( round( $row2[ 'food_gold' ], 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="iron"><?php print( round( $food_iron, 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="coal"><?php print( round( $food_coal, 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="food">1</strong>
							</td>
						</tr>
					</table>
<?php
include( "../global/resources.php" );
?>
					<div class="clear"></div>
					<br />
<?php
if( getCurrentTurn() > 24 ) {
?>
					<div class="place-left half-block">
						<a href="create_trade.php"><strong><?php transprint( 'Create trade offer' ); ?></strong></a> <?php transprint( 'or' ); ?> <a href="manage_auto_trade.php"><strong><?php transprint( 'Manage automatic trade offers' ); ?></strong></a><br />
						<br /> 
						<strong><?php transprint( 'Advice on our currently stockpiled resources' ); ?></strong><br />
<?php
	$result2 = mysql_query( "SELECT gold, iron, coal " .
			"FROM ruler " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] ) or die ( "Query failed: " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$value_gold = $row2[ 'gold' ] * 10;
	$value_iron = $row2[ 'iron' ] * 3;
	$value_coal = $row2[ 'coal' ] * 2;
	$low_gold = $high_gold = $low_iron = $high_iron = $low_coal = $high_coal = false;
	if( $value_gold * 1.4 < ( $value_iron + $value_coal ) * 0.5 ) {
		print( "- " . translate( "We are a bit low on <strong class=\"gold\">gold</strong>, perhaps we can acquire some more." ) . "<br />\r\n" );
		$advice = true;
		$low_gold = true;
	}
	if( $value_gold * 0.6 > ( $value_iron + $value_coal ) * 0.5 ) {
		print( "- " . translate( "We seem to have a lot of <strong class=\"gold\">gold</strong>, perhaps we can trade it for another resource." ) . "<br />\r\n" );
		$advice = true;
		$high_gold = true;
	}
	if( $value_iron * 1.4 < ( $value_gold + $value_coal ) * 0.5 ) {
		print( "- " . translate( "Maybe we should try to stockpile some more <strong class=\"iron\">iron</strong>." ) . "<br />\r\n" );
		$advice = true;
		$low_iron = true;
	}
	if( $value_iron * 0.6 > ( $value_gold + $value_coal ) * 0.5 ) {
		print( "- " . translate( "We have abundant <strong class=\"iron\">iron</strong>, we could sell some of it." ) . "<br />\r\n" );
		$advice = true;
		$high_iron = true;
	}
	if( $value_coal * 1.4 < ( $value_iron + $value_gold ) * 0.5 ) {
		print( "- " . translate( "Our stocks of <strong class=\"coal\">coal</strong> are quite low, we could try to buy some." ) . "<br />\r\n" );
		$advice = true;
		$low_coal = true;
	}
	if( $value_coal * 0.6 > ( $value_iron + $value_gold ) * 0.5 ) {
		print( "- " . translate( "We seem to have a lot of <strong class=\"coal\">coal</strong>, we should try to sell it at a good price." ) . "<br />\r\n" );
		$advice = true;
		$high_coal = true;
	}
	if( !isset( $advice ) ) {
		print( "- " . translate( "Our current stocks of resources seem to be balanced." ) . "<br />\r\n" );
	}
	
	printQuickTrades( $low_gold, $high_gold, $low_iron, $high_iron, $low_coal, $high_coal, $value_gold, $value_iron, $value_coal );
	
	$statement = $dal->prepare( 'SELECT SUM(gold_offer), SUM(iron_offer), SUM(coal_offer), SUM(food_offer), SUM(gold_want), SUM(iron_want), SUM(coal_want), SUM(food_want) 
		FROM trade' );
	$tradeValues = $dal->getArray( $statement );
	//print_r( $tradeValues );
	$statement = $dal->prepare( 'SELECT SUM(gold_offer) + SUM(gold_want), SUM(iron_want) + SUM(iron_offer), SUM(coal_want) + SUM(coal_offer), SUM(food_want) + SUM(food_offer) 
		FROM trade 
		WHERE completed = 1' );
	$traded = $dal->getArray( $statement );
	//print_r( $traded );
?>
					</div>
					<table class="place-right half-block">
						<tr><th colspan="5"><?php transprint( 'Trade volumes' ); ?></th></tr>
						<tr>
							<td></td>
							<td class="align-right">Offered</td>
							<td class="align-right">Requested</td>
							<td class="align-right">Traded</td>
							<td class="align-right">Gold value</td>
						</tr>
						<tr>
							<td><?php transprint( 'Gold' ); ?></td>
							<td class="align-right"><strong class="gold"><?php print( number_format( $tradeValues[ 'SUM(gold_offer)' ] ) ); ?></strong></td>
							<td class="align-right"><strong class="gold"><?php print( number_format( $tradeValues[ 'SUM(gold_want)' ] ) ); ?></strong></td>
							<td class="align-right"><strong class="gold"><?php print( number_format( $traded[ 'SUM(gold_offer) + SUM(gold_want)' ] ) ); ?></strong></td>
							<td class="align-right"><strong class="gold"><?php print( number_format( $traded[ 'SUM(gold_offer) + SUM(gold_want)' ] ) ); ?></strong></td>
						</tr>
						<tr>
							<td><?php transprint( 'Iron' ); ?></td>
							<td class="align-right"><strong class="iron"><?php print( number_format( $tradeValues[ 'SUM(iron_offer)' ] ) ); ?></strong></td>
							<td class="align-right"><strong class="iron"><?php print( number_format( $tradeValues[ 'SUM(iron_want)' ] ) ); ?></strong></td>
							<td class="align-right"><strong class="iron"><?php print( number_format( $traded[ 'SUM(iron_want) + SUM(iron_offer)' ] ) ); ?></strong></td>
							<td class="align-right"><strong class="gold"><?php print( number_format( $traded[ 'SUM(iron_want) + SUM(iron_offer)' ] / $gold_iron ) ); ?></strong></td>
						</tr>
						<tr>
							<td><?php transprint( 'Coal' ); ?></td>
							<td class="align-right"><strong class="coal"><?php print( number_format( $tradeValues[ 'SUM(coal_offer)' ] ) ); ?></strong></td>
							<td class="align-right"><strong class="coal"><?php print( number_format( $tradeValues[ 'SUM(coal_want)' ] ) ); ?></strong></td>
							<td class="align-right"><strong class="coal"><?php print( number_format( $traded[ 'SUM(coal_want) + SUM(coal_offer)' ] ) ); ?></strong></td>
							<td class="align-right"><strong class="gold"><?php print( number_format( $traded[ 'SUM(coal_want) + SUM(coal_offer)' ] / $gold_coal ) ); ?></strong></td>
						</tr>
						<tr>
							<td><?php transprint( 'Food' ); ?></td>
							<td class="align-right"><strong class="food"><?php print( number_format( $tradeValues[ 'SUM(food_offer)' ] ) ); ?></strong></td>
							<td class="align-right"><strong class="food"><?php print( number_format( $tradeValues[ 'SUM(food_want)' ] ) ); ?></strong></td>
							<td class="align-right"><strong class="food"><?php print( number_format( $traded[ 'SUM(food_want) + SUM(food_offer)' ] ) ); ?></strong></td>
							<td class="align-right"><strong class="gold"><?php print( number_format( $traded[ 'SUM(food_want) + SUM(food_offer)' ] / $gold_food ) ); ?></strong></td>
						</tr>
					</table>
					<div class="clear"></div>
<?php
}

if( $premium > 0 ) {
	$offeredShow = formatInput( $_GET[ 'offered' ] );
	if( !isset( $offeredShow ) || $offeredShow == '' ) {
		$offeredShow = 'all';
	}
	$requestedShow = formatInput( $_GET[ 'requested' ] );
	if( !isset( $requestedShow ) || $requestedShow == '' ) {
		$requestedShow = 'all';
	}
?>
					<br />
					<div class="blok">
						<form method="get" action="trade.php">
							<input type="hidden" name="requested" value="<?php print( $requestedShow ); ?>" />
							<?php transprint( 'Show where' ); ?> <?php transprint( 'offered' ); ?>:&nbsp;
							<select name="offered" onchange="this.parentNode.submit();">
								<option value="all"><?php transprint( 'All' ); ?></option>
								<option value="gold"<?php print( $offeredShow == 'gold' ? ' selected="selected"' : '' ); ?>><?php transprint( 'Gold' ); ?></option>
								<option value="iron"<?php print( $offeredShow == 'iron' ? ' selected="selected"' : '' ); ?>><?php transprint( 'Iron' ); ?></option>
								<option value="coal"<?php print( $offeredShow == 'coal' ? ' selected="selected"' : '' ); ?>><?php transprint( 'Coal' ); ?></option>
								<option value="food"<?php print( $offeredShow == 'food' ? ' selected="selected"' : '' ); ?>><?php transprint( 'Food' ); ?></option>
							</select> 
						</form>
						<form method="get" action="trade.php">
							<input type="hidden" name="offered" value="<?php print( $offeredShow ); ?>" />
							<?php transprint( 'and' ); ?>&nbsp;<?php transprint( 'requested' ); ?>:&nbsp;
							<select name="requested" onchange="this.parentNode.submit();">
								<option value="all"><?php transprint( 'All' ); ?></option>
								<option value="gold"<?php print( $requestedShow == 'gold' ? ' selected="selected"' : '' ); ?>><?php transprint( 'Gold' ); ?></option>
								<option value="iron"<?php print( $requestedShow == 'iron' ? ' selected="selected"' : '' ); ?>><?php transprint( 'Iron' ); ?></option>
								<option value="coal"<?php print( $requestedShow == 'coal' ? ' selected="selected"' : '' ); ?>><?php transprint( 'Coal' ); ?></option>
								<option value="food"<?php print( $requestedShow == 'food' ? ' selected="selected"' : '' ); ?>><?php transprint( 'Food' ); ?></option>
							</select> 
						</form>
					</div>
<?php
}
else {
	$offeredShow = 'all';
	$requestedShow = 'all';
}
if( isset( $_SESSION[ 'add_trade_status' ] ) ) {
?>
					<br />
					<div class="blok">
<?php
	print( $_SESSION[ 'add_trade_status' ] );
?>
					</div>
<?php
	unset( $_SESSION[ 'add_trade_status' ] );
}

if( isset( $_SESSION[ 'accept_trade_status' ] ) ) {
?>
					<br />
					<div class="blok">
<?php
	print( $_SESSION[ 'accept_trade_status' ] );
?>
					</div>
<?php
	unset( $_SESSION[ 'accept_trade_status' ] );
}

	$addedConstraint = '';
	if( $offeredShow != 'all' || $requestedShow != 'all' ) {
		if( $offeredShow == 'gold' ) {
			$addedConstraint .= 'AND gold_offer > 0 ';
		}
		elseif( $offeredShow == 'iron' ) {
			$addedConstraint .= 'AND iron_offer > 0 ';
		}
		elseif( $offeredShow == 'coal' ) {
			$addedConstraint .= 'AND coal_offer > 0 ';
		}
		elseif( $offeredShow == 'food' ) {
			$addedConstraint .= 'AND food_offer > 0 ';
		}
		if( $requestedShow == 'gold' ) {
			$addedConstraint .= 'AND gold_want > 0 ';
		}
		elseif( $requestedShow == 'iron' ) {
			$addedConstraint .= 'AND iron_want > 0 ';
		}
		elseif( $requestedShow == 'coal' ) {
			$addedConstraint .= 'AND coal_want > 0 ';
		}
		elseif( $requestedShow == 'food' ) {
			$addedConstraint .= 'AND food_want > 0 ';
		}
	}
	
	$statement = $dal->prepare( 'SELECT * ' .
			'FROM trade ' .
			'WHERE turn + time > :current_turn AND completed = 0 AND private_id = -1 AND faction_id = -1 ' . $addedConstraint .
			'ORDER BY turn + time' );
	$statement->bindParam( ':current_turn', $current_turn, PDO::PARAM_INT );
	$tradeOffers = $dal->getArrayOfObjects( $statement );
	
	if( printTradeOffers( $tradeOffers, 'Public trade offers', $current_turn, $gold_iron, $gold_coal, $gold_food, false, $offeredShow, $requestedShow ) ) {
		$something = true;
	}
	
	$result2 = mysql_query( "SELECT faction_id " .
			"FROM faction_member " .
			"WHERE requested = 0 AND invited = 0 AND ruler_id = " . $_SESSION[ "ruler_id" ] ) or die ( "Query failed: " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	if( isset( $row2[ 'faction_id' ] ) ) {
		$tmp_faction_id = $row2[ 'faction_id' ];
		$statement = $dal->prepare( 'SELECT * ' .
				'FROM trade ' .
				'WHERE turn + time > :current_turn AND completed = 0 AND private_id = -1 AND faction_id = :faction_id ' . $addedConstraint . 
				'ORDER BY turn + time' );
		$statement->bindParam( ':current_turn', $current_turn, PDO::PARAM_INT );
		$statement->bindParam( ':faction_id', $tmp_faction_id, PDO::PARAM_INT );
		$tradeOffers = $dal->getArrayOfObjects( $statement );
		
		if( printTradeOffers( $tradeOffers, 'Faction trade offers', $current_turn, $gold_iron, $gold_coal, $gold_food, false, $offeredShow, $requestedShow ) ) {
			$something = true;
		}
	}
	
	$statement = $dal->prepare( 'SELECT * ' .
			'FROM trade ' .
			'WHERE turn + time > :current_turn AND completed = 0 AND ( private_id = :private OR ( ruler_id = :ruler_id AND private_id != -1 ) ) AND faction_id = -1 ' . $addedConstraint .
			'ORDER BY turn + time' );
	$statement->bindParam( ':current_turn', $current_turn, PDO::PARAM_INT );
	$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
	$statement->bindParam( ':private', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
	$tradeOffers = $dal->getArrayOfObjects( $statement );
	if( printTradeOffers( $tradeOffers, 'Private trade offers', $current_turn, $gold_iron, $gold_coal, $gold_food, true, $offeredShow, $requestedShow ) ) {
		$something = true;
	}
	
	if( !isset( $something ) ) {
 		print( "<br />" .
 				"<div class=\"blok\">\r\n" );
		print( translate( "There are no trade offers." ) . "<br />\r\n" );
		print( "</div>\r\n" );    	
	}
	
	print( "<br />\r\n" );
	print( "<div class=\"blok\">\r\n" );
	print( translate( "Here you can add your own trade offer: <a href=\"create_trade.php\">Make trade offer</a>" ) . "<br />\r\n" .
			translate( "You can acquire resources you are short of and sell resources you have plenty." ) . "<br />\r\n" );
?>
					<br />
					<br />
					<a name="rating-note"></a><strong>*</strong> <?php transprint( 'Note: rating is based on the current market value of resources, as resource prices fluctuate ratings for certain trade offers can change too.' ); ?><br />
				</div>
<?php
aos_footer();

function printTradeOffers( $tradeOffers, $title, $current_turn, $gold_iron, $gold_coal, $gold_food, $private = false, $offeredShow = 'all', $requestedShow = 'all' ) {
	if( count( $tradeOffers ) > 0 ) {
		$something = true;

		$result2 = mysql_query( "SELECT faction_id " .
				"FROM faction " .
				"WHERE leader = " . $_SESSION[ 'ruler_id' ] ) or die ( "Query failed: " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		if( isset( $row2[ 'faction_id' ] ) && !$private ) {
			$faction_leader = true;
			$tmp_faction_id = $row2[ 'faction_id' ];
			$header = "<td>" . translate( "Ruler" ) . "</td><td>" . translate( "Faction" ) . "</td>";
			$total = 9;
		}
		else {
			$header = "<td></td>";
			$faction_leader = false;
			$total = 8;
		}
?>
						<br />
						<table>
							<tr>
								<th colspan="<?php print( $total ); ?>"><?php transprint( $title ); ?></th>
							</tr>
							<tr>
								<td align="right" width="20%"><?php transprint( 'Offered' ); ?></td>
								<td align="right" width="20%"><?php transprint( 'Requested' ); ?></td>
								<td>&nbsp;</td>
								<td><?php transprint( 'Offered by' ); ?></td>
								<td><?php transprint( 'Expires' ); ?></td>
								<td><?php transprint( 'Rating' ); ?> (<a href="#rating-note">*</a>)</td>
								<?php print( $header ); ?>
							</tr>
<?php	
		$count = 0;
		foreach( $tradeOffers AS $tradeOffer ) {
?>
							<tr<?php print( $count % 2 == 0 ? ' class="even"' : '' ); ?>>
<?php							
			if( $tradeOffer->from_faction != -1 ) {
				$result2 = mysql_query( "SELECT name, type_name " .
						"FROM faction " .
						"JOIN faction_types ON faction.faction_type_id = faction_types.faction_type_id " .
						"WHERE faction_id = " . $tradeOffer->from_faction ) or die ( "Query failed: " . mysql_error() );
				$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
				$offered_by = $row2[ 'type_name' ] . ": <a href=\"../overview/faction.php?name=" . $row2[ 'name' ] . "\">" . $row2[ 'name' ] . "</a>";
			}
			else if( $tradeOffer->ruler_id != -1 ) {
				$result2 = mysql_query( "SELECT name " .
						"FROM ruler " .
						"WHERE ruler_id = " . $tradeOffer->ruler_id ) or die ( "Query failed: " . mysql_error() );
				$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
				$offered_by = "<a href=\"../overview/ruler.php?name=" . $row2[ 'name' ] . "\">" . $row2[ 'name' ] . "</a>";
			}
			else {
				$offered_by = translate( "independents" );
			}
			
			$gold_offer = $tradeOffer->gold_offer;
			$total_offer = $gold_offer;
			if( $gold_offer == 0 ) {
				$gold_offer = "";
			}
			else {
				$gold_offer = '<strong class="gold">' . number_format( $gold_offer ) . '</strong><br />';
			}
			
			$iron_offer = $tradeOffer->iron_offer;
			$total_offer += $iron_offer / $gold_iron;
			if( $iron_offer == 0 ) {
				$iron_offer = "";
			}
			else {
				$iron_offer = '<strong class="iron">' . number_format( $iron_offer ) . '</strong><br />';
			}
			
			$coal_offer = $tradeOffer->coal_offer;
			$total_offer += $coal_offer / $gold_coal;
			if( $coal_offer == 0 ) {
				$coal_offer = "";
			}
			else {
				$coal_offer = '<strong class="coal">' . number_format( $coal_offer ) . '</strong><br />';
			}
			
			$food_offer = $tradeOffer->food_offer;
			$total_offer += $food_offer / $gold_food;
			if( $food_offer == 0 ) {
				$food_offer = "";
			}
			else {
				$food_offer = '<strong class="food">' . number_format( $food_offer ) . '</strong><br />';
			}
			
			$gold_want = $tradeOffer->gold_want;
			$total_want = $gold_want;
			if( $gold_want == 0 ) {
				$gold_want = "";
			}
			else {
				$gold_want = '<strong class="gold">' . number_format( $gold_want ) . '</strong><br />';
			}
			
			$iron_want = $tradeOffer->iron_want;
			$total_want += $iron_want / $gold_iron;
			if( $iron_want == 0 ) {
				$iron_want = "";
			}
			else {
				$iron_want = '<strong class="iron">' . number_format( $iron_want ) . '</strong><br />';
			}
			
			$coal_want = $tradeOffer->coal_want;
			$total_want += $coal_want / $gold_coal;
			if( $coal_want == 0 ) {
				$coal_want = "";
			}
			else {
				$coal_want = '<strong class="coal">' . number_format( $coal_want ) . '</strong><br />';
			}			

			$food_want = $tradeOffer->food_want;
			$total_want += $food_want / $gold_food;
			if( $food_want == 0 ) {
				$food_want = "";
			}
			else {
				$food_want = '<strong class="food">' . number_format( $food_want ) . '</strong><br />';
			}			
			
			$expire = ( ( $tradeOffer->turn - $current_turn ) + $tradeOffer->time );
			if( $expire > 1 ) {
				$expire = $expire . " " . translate( "days" );
			}
			else {
				$expire = $expire . " " . translate( "day" );
			}
			
			if( $tradeOffer->ruler_id == $_SESSION[ 'ruler_id' ] ) {
				if( $tradeOffer->accepted == 0 ) {
					$accept_button = translate( "Open" );
				}
				else {
					$accept_button = translate( "Closed" );
				}
			}
			else {
				if( $tradeOffer->accepted == 0 ) {
					$accept_button = "<form action=\"accept_trade.php\" method=\"post\">" .
								"<input type=\"hidden\" name=\"offered_show\" value=\"$offeredShow\" />" .
								"<input type=\"hidden\" name=\"requested_show\" value=\"$requestedShow\" />" .
								"<input type=\"hidden\" value=\"" . $tradeOffer->trade_id . "\" name=\"trade_id\" />" .
								"<input type=\"submit\" value=\"" . translate( "Accept" ) . "\" />" .
							"</form>";
				}
				else {
					$accept_button = translate( "Closed" );
				}
			}
			
			if( $faction_leader ) {
				if( $tradeOffer->from_faction == $tmp_faction_id ) {
					if( $tradeOffer->accepted == 0 ) {
						$faction_accept = "<td>" . translate( "Open" ) . "</td>";
					}
					else {
						$faction_accept = "<td>" . translate( "Closed" ) . "</td>";
					}
				}
				else {
					if( $tradeOffer->accepted == 0 ) {
						$faction_accept = "<td>" .
									"<form action=\"accept_trade.php\" method=\"post\">" .
										"<input type=\"hidden\" value=\"offered_show\" name=\"$offeredShow\" />" .
										"<input type=\"hidden\" value=\"requested_show\" name=\"$requestedShow\" />" .
										"<input type=\"hidden\" value=\"trade\" name=\"faction_trade\" />" .
										"<input type=\"hidden\" value=\"" . $tradeOffer->trade_id . "\" name=\"trade_id\" />" .
										"<input type=\"submit\" value=\"" . translate( "Accept" ) . "\" />" .
									"</form>" .
								"</td>";
					}
					else {
						$faction_accept = "<td>" . translate( "Closed" ) . "</td>";
					}
				}
			}
			else {
				$faction_accept = "";
			}
			
			$offer_quality = $total_offer / $total_want;
			
			if( $offer_quality < 0.90 ) {
				$color = "#EE1111";
				$rating = "very expensive";
			}
			else if( $offer_quality < 0.98 ) {
				$color = "#AA2222";
				$rating = "expensive";
			}
			else if( $offer_quality > 1.10 ) {
				$color = "#33AA33";
				$rating = "very cheap";
			}
			else if( $offer_quality > 1.02 ) {
				$color = "#117711";
				$rating = "cheap";
			}
			else {
				$color = "#2222CC";
				$rating = "fair";
			}
							
?>							
								<td align="right">
									<?php print( $gold_offer ); ?>
						  			<?php print( $iron_offer ); ?>
						  			<?php print( $coal_offer ); ?>
						  			<?php print( $food_offer ); ?>
						  		</td>
						  		<td align="right">
						  			<?php print( $gold_want ); ?>
						  			<?php print( $iron_want ); ?>
						  			<?php print( $coal_want ); ?>
						  			<?php print( $food_want ); ?>
						  		</td>
								<td>&nbsp;</td>
						  		<td><?php print( $offered_by ); ?></td>
								<td><?php print( $expire ); ?></td>
						  		<td style="color: <?php print( $color ); ?>;"><?php transprint( $rating ); ?></td>
						  		<td><?php print( $accept_button ); ?></td>
							   <?php print( $faction_accept ); ?>
							</tr>
<?php
			
			$count ++;
		}
?>
						</table>
<?php
	}
	return $something;
}
?>